package stool.singletonpattern.part4;

/**
 * @Author Felix YF Dong
 * @Date 2021/5/14
 * @Description 测试反射
 */
public class Singleton
{

    private  Singleton() {
        synchronized (Singleton.class) {
            if (!ProtectedFlag.flag) {
                ProtectedFlag.flag = true;
            } else {
                throw new RuntimeException("Singleton refuse！");
            }
        }
    }

    public static Singleton getSingleton(){
        return SingletonHandle.INSTANCE;
    }

    private static class SingletonHandle{
        private static final Singleton INSTANCE = new Singleton();
    }

    private static class ProtectedFlag{
        private static boolean flag = false;
    }
}
